<template>
  <div class="app-container">
    <el-empty description="没有可录入餐厨信息!!!" v-if="!tableQuery.orgnization_id"></el-empty>
    <fill-records ref="table" v-if="tableQuery.orgnization_id"
                  v-show="!showFormView"
                  :tb-loading="tbLoading"
                  :table-page="tablePage"
                  :table-query="tableQuery"
                  @fill-add="addFill"
                  @fill-edit="editFill"
                  @fill-del="delReportData"/>
    <form-panel ref="form" :query="tableQuery" v-show="showFormView" v-if="tableQuery.orgnization_id"/>
    <fill-panel ref="panel" :fill-data="panelData" @callback="reloadTable" @date-change="panelDateChange"/>
    </div>
</template>

<script>
  import {resetTemp,parseTime} from "@/utils"
  import {approvalSteps} from '@/utils/constants'
  import fillPanel from "../fillPanel"
  import fillRecords from "../fillRecords"
  import dailyApi from "@/api/daily_data"
  import moduleApi from "@/api/module"
  import formPanel from "./formPanel"

  export default {
    name: "ccsc-mdl",
    components:{
      fillPanel,
      fillRecords,
      formPanel,
    },
    data(){
      return {
        showFormView:false,
        tablePage: {
          current: null,
          pages: null,
          size: null,
          total: null
        },
        tableQuery:{
          date:null,
          module_name:'food',
          type:'input',
          orgnization_id:null
        },
        tbLoading:{
          loading:false
        },
        temp:{
          fmActNumber:0,
          form_no:null,
          date:'',
          module_name:'food',
          orgnization_id:null,
          values:null,
          status:null,
          type:null
        },
        panelData:{
          dialogStatus:'',
          inputsDialogShow:false,
          temp:{
            fmActNumber:null,
            form_no:null,
            date:null,
            module_name:null,
            orgnization_id:null,
            values:null,
            status:null,
            type:null
          },
          tabsData:[]
        }
      }
    },
    mounted() {
      const query = this.$parent.$parent.$parent.$parent.$data.orgQuery
      this.tableQuery.orgnization_id=query.orgnization_id
      this.tableQuery.date=query.date
    },
    methods:{
        panelSwitch(show){
          this.showFormView=show
          if (show)
            this.$refs.form.fetchData()
        },
        reloadTable(){
          this.$refs.table.fetchData(1)
        },
        orgnizeCheck(query){
          this.tableQuery.orgnization_id=query.orgnization_id
          resetTemp(this.tablePage)
          this.tableQuery.date=query.date
          if (!query.orgnization_id){
            this.$message.warning("请选择组织")
            this.tableData=[]
            return
          }
          this.$nextTick(()=>{
            this.$refs.table.fetchData()
            this.$refs.form.fetchData()
          })
        },
        panelDateChange(date){
          let rqData = Object.assign({},this.tableQuery)
          rqData.date = date
          moduleApi.rptModuleInputs(rqData).then(res=>{
            const allGroups = res.data
            this.panelData.tabsData = this.createdTabsData(allGroups,date)
            this.panelData.temp = Object.assign({},this.temp)
            this.$refs.panel.panelRefresh()
          })
        },
        addFill(){
          this.panelData.dialogStatus='create'
          this.tbLoading.loading=true
          moduleApi.rptModuleInputs(this.tableQuery).then(res=>{
            const allGroups = res.data
            this.panelData.tabsData = this.createdTabsData(allGroups)
            this.panelData.temp = Object.assign({},this.temp)
            this.tbLoading.loading=false
            this.panelData.inputsDialogShow=true
          })
        },
        editFill(idx, row){
          this.panelData.dialogStatus='update'
          this.tbLoading.loading=true
          const rqData={form_no:row.form_no}
          dailyApi.dailyDetail(rqData).then(res=>{
            const allGroups = res.data
            this.panelData.tabsData = this.editedTabsData(allGroups.data)
            this.panelData.temp = Object.assign({},this.temp)
            this.tbLoading.loading=false
            this.panelData.inputsDialogShow=true
          })
        },
        createdTabsData(gData,date=null){
          this.temp.status=null
          this.temp.form_no=null
          this.temp.fmActNumber=0
          if (date)
            this.temp.date=date
          else
            this.temp.date=parseTime(new Date(),'{y}-{m}-{d}')
          this.temp.orgnization_id=this.tableQuery.orgnization_id
          let tagGroups=[]
            for (let gidx in gData){
              const taglist = gData[gidx].list
              let flist = []
              for (let tidx in taglist){
                const tag = taglist[tidx]
                flist.push({
                  cn_name:tag.cn_name,
                  alias:tag.alias?tag.alias:tag.cn_name,
                  messure:tag.messure,
                  dcs_standard_id:tag.id,
                  month_value:tag.month_value,
                  year_value:tag.year_value,
                  is_count:tag.is_count?1:0,
                  is_month:tag.is_month,
                  value:0,
                  desc:tag.description
                })
              }
              tagGroups.push({
                group_name:gData[gidx].group_name,
                list:flist
              })
            }
            return tagGroups
        },
        editedTabsData(gData){
          this.temp.orgnization_id=this.tableQuery.orgnization_id
          let tagGroups=[]
            for (let gidx in gData){
              const taglist = gData[gidx].data
              let flist = []
              for (let tidx in taglist){
                const tag = taglist[tidx]
                this.temp.date=tag.date
                this.temp.form_no=tag.form_no
                this.temp.status=tag.status
                this.temp.fmActNumber = approvalSteps[this.temp.status]
                flist.push({
                  cn_name:tag.dcs_standard_name,
                  alias:tag.alias?tag.alias:tag.dcs_standard_name,
                  messure:tag.messure,
                  dcs_standard_id:tag.dcs_standard_id,
                  month_value:tag.month_value,
                  year_value:tag.year_value,
                  is_count:tag.is_count?1:0,
                  is_month:tag.is_month,
                  value:tag.value,
                  desc:tag.description
                })
              }
              tagGroups.push({
                group_name:gData[gidx].name,
                list:flist
              })
            }
            return tagGroups
        },
        delReportData(idx, row){
          this.$confirm('您确定要永久删除该用户？', '提示', confirm).then(() => {
          dailyApi.dailyDestroy({form_no:row.form_no}).then(res => {
              this.tablePage.total--
              const page_surplus = this.tablePage.total % this.tablePage.size
              const max_page = Math.ceil(this.tablePage.total/this.tablePage.size)
              if (!page_surplus&&this.tablePage.current>max_page)
                this.tablePage.current--
              this.$message.success("删除成功")
              this.$refs.table.fetchData()
            })
          }).catch(() => {
            this.$message.info("已取消删除")
          });
        },

      }

  }
</script>

<style lang="scss" scoped>
  .app-container{
    padding: 0;
  }
  .main-table{
    width: 100%;
  }
  .field-header-col{
    text-align: center;
  }
</style>
